#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :login.py
# @Time      :2021/6/23 23:03
# @Author    : 刘广鸿


from url.urls import URLS
from config.config import shzx_app
from fastapi import Depends, status, Response
from sqlalchemy.orm import Session
from dbs import crud, mSession
from dbs import model
from dbs import schemas
from dbs.database import engine
import uuid
import hashlib
# import datetime
from time import time

"""
    Test for user login by post method：http://127.0.0.1:8000/login
    :param
        class LoginFrom(BaseModel):
            username:str
            password:str
    :return
        200 for logining success
        403 for logining fail
"""


@shzx_app.post(URLS[7])
def login(loginForm: schemas.LoginForm, db: Session = Depends(mSession.get_db)):
    token = crud.login(db, loginForm.username, loginForm.password)
    if token:
        return token
    else:
        return Response(status_code=status.HTTP_403_FORBIDDEN)


"""
    Test for checklogin for get method:http://127.0.0.1:8000/checking/{username}/{token}
    :param
        @username ：for username
        @token：  for the token to Check your identity
    :return
        200 for Sign in effectively
        403 for Invalid login
"""


@shzx_app.get(URLS[9])
def check_login(username: str, token: str, db: Session = Depends(mSession.get_db)):
    if crud.check_token(db, username, token):
        return Response(status_code=status.HTTP_200_OK)
    else:
        return Response(status_code=status.HTTP_403_FORBIDDEN)
